﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using Microsoft.Phone.Controls;
using Microsoft.Practices.ServiceLocation;
using PCManagerReloaded.Model;

namespace PCManagerReloaded.ViewModel
{
    public class WindowsViewModel : ViewModelBase
    {
        public string Title { get { return "PC Manager Reloaded"; } }
        public string PageName { get { return "OS"; } }

        public WindowsViewModel()
        {
            if (IsInDesignMode)
            {
                PcUpTime = "34h 5min 34sec";
                PcStartTime = "01-Jan-2023 23:40";

                ServiceUpTime = "3h 5min 34sec";
                ServiceStartTime = "31-Jan-2023 23:40";

                LastShutdownTime = "34h 5min 34sec";
                LastShutdownStartTime = "34-Jul-2023 23:40";

                IsShutdownRequested = true;
                ShutdownStatus = "Is requested";
            }
            else
            {
                IsShutdownRequested = false;
                this.Refresh();
            }
        }

        public void Refresh()
        {
            this.IsBusy = true;

            var modelProvider = ServiceLocator.Current.GetInstance<FileModelProvider>();

            LoadPcUpTime(modelProvider);
            LoadServiceUpTime(modelProvider);
            LoadLastShutdownTime(modelProvider);
            //LoadIsShutdownRequested(modelProvider);
        }

        private void LoadPcUpTime(FileModelProvider modelProvider)
        {
            try
            {
                modelProvider.LoadPCStartUpTime(args =>
                                                    {
                                                        IsBusy = false;

                                                        if (args.Error != null)
                                                        {
                                                            var messageBox = new CustomMessageBox()
                                                                                 {
                                                                                     Caption = "Exception occurred",
                                                                                     Message = args.Error.Message,
                                                                                     LeftButtonContent = "Try again",
                                                                                     RightButtonContent = "Cancel",
                                                                                     IsFullScreen = true
                                                                                 };
                                                            messageBox.Dismissed += (s1, e1) =>
                                                                                        {
                                                                                            switch (e1.Result)
                                                                                            {
                                                                                                case
                                                                                                    CustomMessageBoxResult.
                                                                                                        LeftButton:
                                                                                                    // Do something.
                                                                                                    LoadPcUpTime(modelProvider);
                                                                                                    break;
                                                                                                case
                                                                                                    CustomMessageBoxResult.
                                                                                                        RightButton:
                                                                                                    // Do something.
                                                                                                    break;
                                                                                                case CustomMessageBoxResult.None
                                                                                                    :
                                                                                                    // Do something.
                                                                                                    break;
                                                                                                default:
                                                                                                    break;
                                                                                            }
                                                                                        };

                                                            messageBox.Show();
                                                        }
                                                        if (args.Result != TimeSpan.Zero)
                                                        {
                                                            WindowsNotificationMessage = "Online";
                                                            SetPcUpTimes(args.Result);
                                                        }
                                                        else
                                                        {
                                                            WindowsNotificationMessage = "Not Online";
                                                        }


                                                        this.IsBusy = false;
                                                    });
            }
            catch (Exception)
            {
            }
        }

        //private void LoadIsShutdownRequested(FileModelProvider modelProvider)
        //{
        //    try
        //    {
        //        modelProvider.LoadIsShutdownRequested(args =>
        //        {
        //            IsBusy = false;

        //            if (args.Error != null)
        //            {
        //                var messageBox = new CustomMessageBox()
        //                {
        //                    Caption = "Exception occurred",
        //                    Message = args.Error.Message,
        //                    LeftButtonContent = "Try again",
        //                    RightButtonContent = "Cancel",
        //                    IsFullScreen = true
        //                };
        //                messageBox.Dismissed += (s1, e1) =>
        //                {
        //                    switch (e1.Result)
        //                    {
        //                        case
        //                            CustomMessageBoxResult.
        //                                LeftButton:
        //                            // Do something.
        //                            LoadIsShutdownRequested(modelProvider);
        //                            break;
        //                        case
        //                            CustomMessageBoxResult.
        //                                RightButton:
        //                            // Do something.
        //                            break;
        //                        case CustomMessageBoxResult.None
        //                            :
        //                            // Do something.
        //                            break;
        //                        default:
        //                            break;
        //                    }
        //                };

        //                messageBox.Show();
        //            }

        //                WindowsNotificationMessage = "Online";
        //                IsShutdownRequested = args.Result;
                        


        //            this.IsBusy = false;
        //        });
        //    }
        //    catch (Exception)
        //    {
        //    }
        //}

        private void SetShutdownText(bool result)
        {
            DateTime dateTime = DateTime.Now + new TimeSpan(0,5,0);
            ShutdownStatus = result ? dateTime.ToString("HH:mm:ss") : "is not requested";
        }

        private void LoadServiceUpTime(FileModelProvider modelProvider)
        {
            try
            {
                modelProvider.LoadServiceStartUpTime(args =>
                {
                    IsBusy = false;

                    if (args.Error != null)
                    {
                        var messageBox = new CustomMessageBox()
                        {
                            Caption = "Exception occurred",
                            Message = args.Error.Message,
                            LeftButtonContent = "Try again",
                            RightButtonContent = "Cancel",
                            IsFullScreen = true
                        };
                        messageBox.Dismissed += (s1, e1) =>
                        {
                            switch (e1.Result)
                            {
                                case
                                    CustomMessageBoxResult.
                                        LeftButton:
                                    // Do something.
                                    LoadServiceUpTime(modelProvider);
                                    break;
                                case
                                    CustomMessageBoxResult.
                                        RightButton:
                                    // Do something.
                                    break;
                                case CustomMessageBoxResult.None
                                    :
                                    // Do something.
                                    break;
                                default:
                                    break;
                            }
                        };

                        messageBox.Show();
                    }
                    
                    if (args.Result != DateTime.MinValue)
                    {
                        WindowsNotificationMessage = "Online";
                        SetServiceUpTimes(args.Result);
                    }
                    else
                    {
                        WindowsNotificationMessage = "Not Online";
                    }


                    this.IsBusy = false;
                });
            }
            catch (Exception)
            {
            }
        }

        private void LoadLastShutdownTime(FileModelProvider modelProvider)
        {
            try
            {
                modelProvider.LoadLastSystemShutdown(args =>
                {
                    IsBusy = false;

                    if (args.Error != null)
                    {
                        var messageBox = new CustomMessageBox()
                        {
                            Caption = "Exception occurred",
                            Message = args.Error.Message,
                            LeftButtonContent = "Try again",
                            RightButtonContent = "Cancel",
                            IsFullScreen = true
                        };
                        messageBox.Dismissed += (s1, e1) =>
                        {
                            switch (e1.Result)
                            {
                                case
                                    CustomMessageBoxResult.
                                        LeftButton:
                                    // Do something.
                                    LoadLastShutdownTime(modelProvider);
                                    break;
                                case
                                    CustomMessageBoxResult.
                                        RightButton:
                                    // Do something.
                                    break;
                                case CustomMessageBoxResult.None
                                    :
                                    // Do something.
                                    break;
                                default:
                                    break;
                            }
                        };

                        messageBox.Show();
                    }

                    if (args.Result != DateTime.MinValue)
                    {
                        WindowsNotificationMessage = "Online";
                        SetShutdownTimes(args.Result);
                    }
                    else
                    {
                        WindowsNotificationMessage = "Not Online";
                    }


                    this.IsBusy = false;
                });
            }
            catch (Exception)
            {
            }
        }

        private void SetShutdownTimes(DateTime result)
        {
            DateTime now = DateTime.Now;
            TimeSpan timeSpan = now - result;
            LastShutdownTime = string.Format("{0}h {1}m {2}s", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
            LastShutdownStartTime = result.ToString("dd-MMM-yyyy HH:mm");
        }

        private void SetServiceUpTimes(DateTime result)
        {
            DateTime now = DateTime.Now;
            TimeSpan timeSpan = now - result;
            ServiceStartTime = string.Format("{0}h {1}m {2}s", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
            ServiceUpTime = result.ToString("dd-MMM-yyyy HH:mm");
        }

        private void SetPcUpTimes(TimeSpan result)
        {
            DateTime now = DateTime.Now;
            PcStartTime = string.Format("{0}h {1}m {2}s", result.Hours, result.Minutes, result.Seconds);
            DateTime dateTime = now - result;
            PcUpTime = dateTime.ToString("dd-MMM-yyyy HH:mm");
        }

        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                RaisePropertyChanged(()=>IsBusy);
                RaisePropertyChanged(() => IsNotBusy);
            }
        }

        public bool IsNotBusy
        {
            get { return !_isBusy; }
           
        }

        public string ShutdownStatus
        {
            get { return _shutdownStatus; }
            set
            {
                _shutdownStatus = value;
                RaisePropertyChanged(()=>ShutdownStatus);
            }
        }

        public bool IsShutdownRequested
        {
            get { return _isShutdownRequested; }
            set
            {
                
                _isShutdownRequested = value;
                SetShutdownText(_isShutdownRequested);
                RaisePropertyChanged(()=>IsShutdownRequested);
            }
        }

        public void RequestShutdown()
        {
            this.IsBusy = true;

            var modelProvider = ServiceLocator.Current.GetInstance<FileModelProvider>();

            try
            {
                modelProvider.RequestShutdown(args =>
                {
                    IsBusy = false;

                    if (args.Error != null)
                    {
                        var messageBox = new CustomMessageBox()
                        {
                            Caption = "Exception occurred",
                            Message = args.Error.Message,
                            LeftButtonContent = "Try again",
                            RightButtonContent = "Cancel",
                            IsFullScreen = true
                        };
                        messageBox.Dismissed += (s1, e1) =>
                        {
                            switch (e1.Result)
                            {
                                case
                                    CustomMessageBoxResult.
                                        LeftButton:
                                    // Do something.
                                    RequestShutdown();
                                    break;
                                case
                                    CustomMessageBoxResult.
                                        RightButton:
                                    // Do something.
                                    break;
                                case CustomMessageBoxResult.None
                                    :
                                    // Do something.
                                    break;
                                default:
                                    break;
                            }
                        };

                        messageBox.Show();
                    }

                    WindowsNotificationMessage = "Online";
                    IsShutdownRequested = true;
                    //LoadIsShutdownRequested(modelProvider);

                    this.IsBusy = false;
                });
            }
            catch (Exception)
            {
            }
        }

        public void CancelShutdown()
        {
            this.IsBusy = true;

            var modelProvider = ServiceLocator.Current.GetInstance<FileModelProvider>();

            try
            {
                modelProvider.CancelShutdown(args =>
                {
                    IsBusy = false;

                    if (args.Error != null)
                    {
                        var messageBox = new CustomMessageBox()
                        {
                            Caption = "Exception occurred",
                            Message = args.Error.Message,
                            LeftButtonContent = "Try again",
                            RightButtonContent = "Cancel",
                            IsFullScreen = true
                        };
                        messageBox.Dismissed += (s1, e1) =>
                        {
                            switch (e1.Result)
                            {
                                case
                                    CustomMessageBoxResult.
                                        LeftButton:
                                    // Do something.
                                    RequestShutdown();
                                    break;
                                case
                                    CustomMessageBoxResult.
                                        RightButton:
                                    // Do something.
                                    break;
                                case CustomMessageBoxResult.None
                                    :
                                    // Do something.
                                    break;
                                default:
                                    break;
                            }
                        };

                        messageBox.Show();
                    }

                    WindowsNotificationMessage = "Online";
                    IsShutdownRequested = false;

                    this.IsBusy = false;
                });
            }
            catch (Exception)
            {
            }
        }
        public string LastShutdownStartTime
        {
            get { return _lastShutdownStartTime; }
            set
            {
                _lastShutdownStartTime = value;
                RaisePropertyChanged(()=>LastShutdownStartTime);
            }
        }

        public string ServiceStartTime
        {
            get { return _serviceStartTime; }
            set
            {
                _serviceStartTime = value;
                RaisePropertyChanged(() => ServiceStartTime);
            }
        }

        public string PcStartTime
        {
            get { return _pcStartTime; }
            set
            {
                _pcStartTime = value;
                RaisePropertyChanged(() => PcStartTime);
            }
        }

        public string LastShutdownTime
        {
            get { return _lastShutdownTime; }
            set
            {
                _lastShutdownTime = value;
                RaisePropertyChanged(() => LastShutdownTime);
            }
        }

        public string ServiceUpTime
        {
            get { return _serviceUpTime; }
            set
            {
                _serviceUpTime = value;
                RaisePropertyChanged(() => ServiceUpTime);
            }
        }

        public string PcUpTime
        {
            get { return _pcUpTime; }
            set
            {
                _pcUpTime = value;
                RaisePropertyChanged(()=>PcUpTime);
            }
        }

        private string _windowsNotificationMessage;
        private string _pcUpTime;
        private string _serviceUpTime;
        private string _lastShutdownTime;
        private string _pcStartTime;
        private string _serviceStartTime;
        private string _lastShutdownStartTime;
        private bool _isShutdownRequested;
        private string _shutdownStatus;
        private bool _isBusy;

        public string WindowsNotificationMessage
        {
            get { return _windowsNotificationMessage; }
            set
            {
                _windowsNotificationMessage = value;
                RaisePropertyChanged(() => WindowsNotificationMessage);
            }
        }
    }
}
